package ru.budget.mobile.data.repository

import ru.budget.mobile.core.Response
import ru.budget.mobile.data.entities.ConsumptionData
import ru.budget.mobile.data.sources.ConsumptionLocalSource
import ru.budget.mobile.domain.entities.ConsumptionDomain
import ru.budget.mobile.domain.repository.ConsumptionRepository

class ConsumptionRepositoryImpl(
    private val local: ConsumptionLocalSource
): ConsumptionRepository {
    override suspend fun consumptionList(): Response<List<ConsumptionDomain>> =
        local.consumptionList().map {
            it.map(mapper)
        }

    override suspend fun addConsumption(consumption: ConsumptionDomain): Response<Boolean> =
        local.addConsumption(
            consumption = ConsumptionData(
                id = consumption.id,
                title = consumption.title,
                type = consumption.type,
                category = consumption.category,
                sum = consumption.sum,
                datetime = consumption.datetime
            )
        )

    private val mapper = { consumption: ConsumptionData ->
        ConsumptionDomain(
            id = consumption.id,
            title = consumption.title,
            type = consumption.type,
            category = consumption.category,
            sum = consumption.sum,
            datetime = consumption.datetime
        )
    }
}